package com.woniu.controller;

import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.woniu.pojo.Staff;
import com.woniu.service.StaffService;
@Controller
@RequestMapping("/login")
public class LoginController {
	@Autowired
	private StaffService staffService;
	@ResponseBody
	@RequestMapping("/dologin")
	public String login(String job,String passWord,@RequestParam(value="isRemenberMe",defaultValue = "0")Integer isRemenberMe) {
		System.out.println("-----");
	
		Subject subject = SecurityUtils.getSubject();
		UsernamePasswordToken token = new UsernamePasswordToken(job, passWord);
		System.out.println(isRemenberMe+"-----");
		if(isRemenberMe==1){
			System.out.println(isRemenberMe);
	        token.setRememberMe(true);
	    }

		try {
			subject.login(token);
			Staff loginStaff = (Staff)subject.getPrincipal();
			System.out.println(loginStaff);
			Session session=subject.getSession();
			session.setAttribute("loginStaff", loginStaff);
			//List<Role> roles=loginUser.getRoles();
			//return "/index.jsp";
			return "登录成功";
		} catch (AuthenticationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "登录失败";
			//return "redirect:/login.jsp";
		}
	}

}

